package com.seari.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.seari.bean.PersonInfoBean;
import com.seari.bean.PlanShiftInfoBean;
import com.seari.bean.ReqPlanShiftBean;
import com.seari.kafka.TransferWorker;
import com.seari.scheduling.plan.PlanFourClass;
import com.seari.scheduling.plan.PlanMonMidClass;
import com.seari.scheduling.plan.PlanMonNigClass;

@Service
public class PlanSchedulingServiceImpl implements PlanSchedulingService
{
	
	protected static Logger logger = LoggerFactory.getLogger(PlanSchedulingServiceImpl.class);
	
	//排班表具体规则实现实例
	@Autowired
	PlanFourClass fourClassInstance;   //大四班

	@Autowired
	PlanMonMidClass monMidClassinstance; //早中班

	@Autowired
	PlanMonNigClass monNigClassinstance; //做一休一

	//人员信息
	@Autowired
	PerSonInfoService perSonInfoService;
	
	//排班数据
	@Autowired
	PlanShiftInfoService planShiftInfoService;

	@Override
	public List<Object> fetchPlanSchedulingViewData(ReqPlanShiftBean bean)
	{
		logger.info("fecth station staff info ...");
		List<PersonInfoBean> perSonInfoList = perSonInfoService.fetchPerSonInfoList();
		
		//
		String planShiftDate = bean.getReqShiftDate(); 

		Map<String, List<PersonInfoBean>> classify = new HashMap<>();
		
		//按照岗位对人员分类
		logger.info("Classify the staff by post ...");
		for (PersonInfoBean personInfoBean : perSonInfoList)
		{
			if (classify.containsKey(personInfoBean.getPostInfo()))
			{
				classify.get(personInfoBean.getPostInfo()).add(personInfoBean);
			} else
			{
				List<PersonInfoBean> list = new ArrayList<>();
				list.add(personInfoBean);
				classify.put(personInfoBean.getPostInfo(), list);
			}
		}
		
		perSonInfoList.clear();

		List<Object> res = new ArrayList<>();
		
		//根据岗位的排班规则生成排班数据
		logger.info("Generate scheduling data ...");
		Iterator<Entry<String, List<PersonInfoBean>>> iterator = classify.entrySet().iterator();
		while (iterator.hasNext())
		{
			Map<String,Object> item = new HashMap<>();
			Entry<String, List<PersonInfoBean>> next = iterator.next();
			List<PersonInfoBean> value = next.getValue();
			String type = value.get(0).getShift();
			switch (type)
			{
			case "fourClass":
				List<PersonInfoBean> fourClassData = fourClassInstance.planScheduling(bean, value);
				item.put("dept", next.getKey());
				item.put("data", fourClassData);
				perSonInfoList.addAll(fourClassData);
				res.add(item);
				break;

			case "monMid":
				List<PersonInfoBean> monMidData = monMidClassinstance.planScheduling(bean, value);
				item.put("dept", next.getKey());
				item.put("data", monMidData);
				perSonInfoList.addAll(monMidData);
				res.add(item);
				break;

			case "monNig":
				List<PersonInfoBean> monNigData = monNigClassinstance.planScheduling(bean, value);
				item.put("dept", next.getKey());
				item.put("data", monNigData);
				perSonInfoList.addAll(monNigData);
				res.add(item);
				break;
			default:
				break;
			}
		}
		
		logger.info("insert scheduling data into database ...");
		List<PlanShiftInfoBean> shiftInfoData = this.insertShiftInfoData(perSonInfoList, planShiftDate);  //插入人员排班数据
		
		
		//发送消息队列
		logger.info("transpond message ...");
		TransferWorker worker = new TransferWorker();
		worker.setTopic("schedule_info");
		worker.setData(shiftInfoData);
		worker.setType("排班信息");
		new Thread(worker).start();
		
		System.out.println("shiftInfoData : " + JSON.toJSONString(shiftInfoData));
		
		
		String jsonString = JSON.toJSONString(perSonInfoList);
		System.out.println(jsonString);
		
		return res;
	}
	
	
	
	/**
	 * 组装要插入的人员排班数据
	 * @param list
	 * @param planShiftDate
	 */
	public List<PlanShiftInfoBean> insertShiftInfoData(List<PersonInfoBean> list,String planShiftDate) {
		List<PlanShiftInfoBean> shiftInfos = new ArrayList<>();
		for (PersonInfoBean personInfoBean : list)
		{
			PlanShiftInfoBean shiftInfo = new PlanShiftInfoBean();
			shiftInfo.setPid(personInfoBean.getUid());
			shiftInfo.setPlanShiftDate(planShiftDate);
			shiftInfo.setPlanShiftInfo(StringUtils.join(personInfoBean.getShiftInfo(),","));
			shiftInfos.add(shiftInfo);
		}
		
		planShiftInfoService.insertPlanShiftInfo(shiftInfos);
		
		return shiftInfos;
	}
}
